package com.alan.annotation.validation.aspect;

import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 
 * @author alan
 * @version 创建时间：2017年3月16日 上午11:22:27
 */
public class ControllerLogAspect {
	
	/**
	 * 日志对象
	 */
	private Logger logger = LoggerFactory.getLogger(ControllerLogAspect.class);
	
	
	private void before(JoinPoint joinPoint){
		logger.info("【请求方法:】 "+joinPoint.getTarget().getClass().getSimpleName()+"."+joinPoint.getSignature().getName());
		//logger.info("【请求入参:】 "+Arrays.toString(joinPoint.getArgs()));
		StringBuffer sb = new StringBuffer();
		String str = "";
		sb.append("[");
		Object[] objects = joinPoint.getArgs();
		for(int i = 0; i < objects.length; i++){
			if(objects[i] instanceof HttpServletRequest ||
					objects[i] instanceof HttpServletResponse ||
					objects[i] instanceof HttpSession ){
				continue;
			}else if(objects[i] instanceof  String ){
				sb.append("String:"+objects[i].toString()+",");
			}else if(objects[i] instanceof  Integer){
				sb.append("Integer:"+objects[i].toString()+",");
			}else{
				if( objects[i] != null){
					sb.append(objects[i].getClass().getSimpleName()+":"+JSON.toJSONString(objects[i])+",");
				}
			}
		}
		str = sb.substring(0,sb.length()-1);
		str = str + "]";
		logger.info("【请求入参:】"+str);
	}
	
	public void AfterReturning(JoinPoint point, Object returnValue){
		logger.info("【返回方法:】"+point.getSignature().getName());
		if(returnValue instanceof String){
			logger.info("【返回参数:】" + returnValue);
		}else{
			logger.info("【返回参数:】" + JSON.toJSONString(returnValue));
		}
	}
	

   
}
